﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.Common;
using StoragePeriod.Data;
using StoragePeriod.Web.Common;
using StoragePeriod.Model.RawDict;
using StoragePeriod.BLL.RawDict;

namespace StoragePeriod.Web.SysManage
{
    public partial class RawNormManage : SecurityPage
    {
        T_RawMaterial tRawMaterial = new T_RawMaterial();//原料操作

        public override string PrivCode()
        {
            return "Default";
        }

        int i = 0;
        public string GetXH()
        {
            i += 1;
            return i.ToString();
        }

        public void BindTreeView()
        {
            tvRawView.Nodes.Clear();
            List<RawMaterialType> RawTypeList = RawMaterialType.Where("ParentId=@0 and Typeinfoid=@1", 0, Convert.ToInt32(EnumData.TypeInfo.食品检测)).ToList();
            RawTypeList.ForEach(p =>
            {
                TreeNode childNode = new TreeNode()
                {
                    Text = p.TypeName,
                    Value = p.id.EToString(),
                    Target = "Parent",
                };
                childNode = GetRawByTypeId(childNode);
                tvRawView.Nodes.Add(childNode);
            });
            tvRawView_SelectedNodeChanged(null, null);
        }

        private TreeNode GetRawByTypeId(TreeNode tn)
        {
            int Pid = int.Parse(tn.Value);
            List<RawMaterialType> RawTypeList = RawMaterialType.Where("ParentId=@0", Pid).ToList();
            RawTypeList.ForEach(p =>
            {
                TreeNode childNode = new TreeNode()
                {
                    Text = p.TypeName,
                    Value = p.id.ToString(),
                    Target = "Parent"
                };
                childNode = GetRawByTypeId(childNode);
                tn.ChildNodes.Add(childNode);
            });

            int TypeId = int.Parse(tn.Value);
            List<E_RawMaterial> rmlist = rmlist = tRawMaterial.GetModelList("PeriodTypeStorageID ="+TypeId);

            rmlist.ForEach(a =>
            {
                TreeNode rnod = new TreeNode()
                {
                    Text = a.RAWName,
                    Value = a.id.EToString(),
                    Target = "Child"
                };
                tn.ChildNodes.Add(rnod);
            });
            return tn;
        }

        private void DropDataBing()
        {
            List<RawMaterialType> rawType = RawMaterialType.Where("ParentId=@0 and Typeinfoid=@1", 0, Convert.ToInt32(EnumData.TypeInfo.预测指标)).ToList();
            this.Drop_OntNorm.DataSource = rawType;
            this.Drop_OntNorm.DataValueField = "id";
            this.Drop_OntNorm.DataTextField = "TypeName";
            this.Drop_OntNorm.DataBind();
            this.Drop_OntNorm.SelectedIndex = 0;

            if (this.Drop_OntNorm.SelectedItem != null)
            {
                int One_Norm =Convert.ToInt32(this.Drop_OntNorm.SelectedValue);
                List<RawMaterialType> TypeList = RawMaterialType.Where("ParentId=@0", One_Norm).ToList();
                this.Drop_TwoNorm.DataSource = TypeList;
                this.Drop_TwoNorm.DataValueField = "id";
                this.Drop_TwoNorm.DataTextField = "TypeName";
                this.Drop_TwoNorm.DataBind();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            DropDataBing();
            BindTreeView();
        }

        protected void btPost_Click(object sender, EventArgs e)
        {
            if (this.HidIsEdit.Value == "0")
            {
                //添加
                int TwoNormId = Convert.ToInt32(this.HidNromTypeId.Value);
                RawMaterialType md_Type = RawMaterialType.Where("id=@0", TwoNormId).First();
                RawMaterialType md_TypeParent = RawMaterialType.Where("id=@0", md_Type.ParentId).First();

                RawNorm md = new RawNorm();
                md.ElectValue = this.txt_ElectValue.Text.Trim();
                md.RawId =Convert.ToInt32(this.tvRawView.SelectedNode.Value);
                md.OneNormName = md_TypeParent.TypeName;
                md.TwoNormName = md_Type.TypeName;
                md.NormId = TwoNormId;
                RawNorm.Insert(md);
            }
            else
            {
                int NormId = Convert.ToInt32(this.HidRawNronId.Value);
                int TwoNormId = Convert.ToInt32(this.HidNromTypeId.Value);
                RawMaterialType md_Type = RawMaterialType.Where("id=@0", TwoNormId).First();
                RawMaterialType md_TypeParent = RawMaterialType.Where("id=@0", md_Type.ParentId).First();
                RawNorm md = RawNorm.Where("id=@0", NormId).First();
                md.ElectValue = this.txt_ElectValue.Text.Trim();
                md.RawId = Convert.ToInt32(this.tvRawView.SelectedNode.Value);
                md.OneNormName = md_TypeParent.TypeName;
                md.TwoNormName = md_Type.TypeName;
                md.NormId = TwoNormId;
                RawNorm.Update(md, "id=@0", md.id);
            }
            this.tvRawView_SelectedNodeChanged(null, null);
        }

        protected void RepDataList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int RawNornId = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == "Del")
            {
                RawNorm.Delete("id=@0", RawNornId);
                this.tvRawView_SelectedNodeChanged(null, null);
            }
            else
            {
                int OneNrom=0;
                int TwoNrom=0;
                RawNorm md = RawNorm.Where("id=@0", RawNornId).First();
                this.HidRawNronId.Value = md.id.ToString();
                RawMaterialType md_TwoNormType = RawMaterialType.Where("id=@0", md.NormId).FirstOrDefault();
                if (md_TwoNormType != null)
                {
                    OneNrom = Convert.ToInt32(md_TwoNormType.ParentId);
                    TwoNrom = md.NormId;
                }
                UtilityUI.Load(this.upPanelMain, "LoadData(" + OneNrom.ToString() + "," + TwoNrom.ToString() + ",'" + md.ElectValue + "');ShowDlg(1)");
            }
        }

        protected void tvRawView_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (this.tvRawView.SelectedNode == null) return;

            if (this.tvRawView.SelectedNode.Target != "Parent")
            {
                this.Lab_RawName.Text = this.tvRawView.SelectedNode.Text;
                this.HidSelectRawId.Value = this.tvRawView.SelectedNode.Value;

                List<RawNorm> list = RawNorm.Where("RawId=@0", int.Parse(this.tvRawView.SelectedNode.Value)).ToList();
                this.RepDataList.DataSource = list;
                this.RepDataList.DataBind();
            }
        }

    }
}